home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / VideoToolboxSources / MoveMouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-11  |  7.3 KB  |  183 lines  |  [TEXT/MMCC]

  1. /*
  2. MoveMouse.c
  3.  
  4. Dan Sears (sears@netcom.com) writes, 
  5. "The Apple file MoveMouse.c is based on some code from Jon Wtte and it uses the
  6. Cursor Device Manager.  You should read the tech note that describes the
  7. API for this manager, "HW 01 - ADB (The Untold Story: Space Aliens ate my
  8. mouse)". 
  9.  
  10. This is from Apple. It may be useful to people who need an equivalent to
  11. SetMouse.c that works on ALL Macs. - dgp, 12/94
  12.  
  13. NOTE: The THINK C 6 compiler choked on one of the (legal) recursive structure
  14. definitions in the original version of this file. (The structure definitions
  15. are now all in Apple's CursorDevices.h file.) The THINK C 7 and CodeWarrior
  16. compilers don't complain.
  17.  
  18. From Apple Developer Services:
  19.  
  20. In the past, moving the cursor programatically on the Macintosh has
  21. entailed the manipulation of low memory globals. This has worked for
  22. all macs for quite some time. But with the advent of the power book
  23. series and many 3rd party track balls, it has become apparent that the
  24. low memory globals no longer contained adequate information for the
  25. system to truly support all types of positioning devices. So, with the
  26. new Centris CPU's, and CPU's from all lines after we are implementing a
  27. new tool set that provides a much more comprehensive set of utilities
  28. for dealing with the cursor location and mouse button state. We call
  29. this new manager the Cursor Device Manager, and DTS will be documenting
  30. it in a future tech not, but since many machines are now shipping that
  31. do not use the traditional low memory globals to get the mouse location
  32. and position the mouse, it is important to "leak" a little of the tech
  33. note information early.
  34.  
  35. First, the CDM (Cursor device manager) has its own trap ($AADB) and you
  36. can determine if a machine supports the CDM by using the standard TrapAvailable
  37. routine. (See inside mac for the source code to Trap
  38. Available). Once you have determined that the CDM exists, you should
  39. try not to use the low memory globals RawMouse and MTemp to get the
  40. current cursor location, or set a new cursor location. Instead the CDM
  41. provides 3 calls which you can use to get this info, the first is
  42. CrsrDevNextDevice which will give you the next cursor device record in
  43. the CDM list, if you pass it a NULL, it will give you the head of the
  44. list. Once you have the first cursor device you can use it to get the
  45. Cursor data record which contains the current cursor location in its
  46. where field. You can set the current location with either the
  47. CrsrDevMove (which moves relative to the current location) or the
  48. CrsrDevMoveTo (which moves to an absolute location) routines.
  49.  
  50. The following is some sample code which implement some generic routines
  51. that work on all macintoshes. These routines are written not for speed
  52. but for clarity, so if you are writing a device driver or time critical
  53. code there are some things that you can do to speed up these routines,
  54. like predetermine at the outset if the CDM exists, this way you
  55. wouldn't have to check every time you want to use it, and getting the
  56. cursor device only once and keeping the pointer to it around between
  57. calls.
  58.  
  59. Anyway, here is some sample code that moves the mouse diagonally until
  60. the mouse button is pressed. For a further explanation of how the older
  61. low memory global method works, see the developer support tech info
  62. database on AppleLink.
  63.  
  64. HISTORY:
  65. 8/94 dgp Downloaded from ftp://nada.kth.se/pub/hacks/mac-faq/MoveMouse.c
  66. 10/94 dgp made minimal changes so that it would compile in CodeWarrior C. Untested.
  67. 12/94 dgp updated for compatibility with Universal Header "CursorDevices.h". Untested.
  68. 1/5/95 dgp updated for compatiblity with Universal Headers 2, in which CursorDevices.h was
  69. extensively changed. Untested.
  70. */
  71.  
  72. #include "VideoToolbox.h"    // TrapAvailable
  73. #if !UNIVERSAL_HEADERS
  74.     #error "Sorry, this file cannot be compiled without Apple's Universal Header files, preferably v. 2"
  75. #endif
  76. #include <CursorDevices.h>
  77. #define _CursorDeviceDispatch 0xAADB // new cursor device manager trap 
  78. #if __powerc
  79.     #define CallCursorTask()    // not needed on powerpc, so give empty definition
  80. #else
  81.     pascal void CallCursorTask(void) = {0x2078,0x08EE,0x4E90};
  82.     /*
  83.         MOVE.L jCrsrTask,A0
  84.         JSR (A0)
  85.     */
  86. #endif
  87. // Old style mouse moving equates for the low memory globals
  88. #define xRawMouse 0x082C    // low memory global that has current mouse loc
  89. #define xMTemp 0x0828 // low memory global that has current mouse loc
  90. #define xCrsrNew 0x08CE // set after you change mtemp and rawmouse
  91. #define xCrsrCouple 0x08CF // true if the cursor is tied to the mouse
  92.  
  93. void GetMouseDevicePosition(Point *currentPoint);
  94. void SetMouseDevicePosition(Point newPoint);
  95. void SetMouseDevicePositionRel(Point newPoint);
  96. void FudgeCursor(void);
  97.  
  98. #if !defined(UNIVERSAL_HEADERS) || UNIVERSAL_HEADERS<2
  99.     #define CursorDevice CrsrDevice
  100.     #define CursorDeviceNextDevice CrsrDevNextDevice
  101.     #define CursorDeviceMove CrsrDevMove
  102.     #define CursorDeviceMoveTo CrsrDevMoveTo
  103. #endif
  104.  
  105. void GetMouseDevicePosition(Point *currentPoint)
  106. {
  107.     // This routine returns the current cursor location
  108.     CursorDevice *firstMouse;
  109.     if (TrapAvailable(_CursorDeviceDispatch)) { 
  110.         // If we get here we have the CDM
  111.         firstMouse = NULL;    // start at head of cursor dev list
  112.         CursorDeviceNextDevice(&firstMouse);    // get the next cursor device
  113.         // Now get the current cursor location from the Cursor data
  114.         *currentPoint = firstMouse->whichCursor->where;
  115.     } else {
  116.         // No CDM so we use the low memory global
  117.         *currentPoint = *(Point *)xRawMouse;
  118.     }
  119. }
  120.  
  121. void SetMouseDevicePosition(Point newPoint)
  122. {
  123.     // This routine sets the mouse position to the passed point
  124.     CursorDevice *firstMouse;
  125.  
  126.     if (TrapAvailable(_CursorDeviceDispatch)) { 
  127.         // If we get here we have the CDM
  128.         firstMouse = NULL;    // start at head of cursor dev list
  129.         CursorDeviceNextDevice(&firstMouse); // get the next cursor device
  130.         // Call CDM to move the mouse
  131.         CursorDeviceMoveTo(firstMouse,(long)newPoint.h,(long)newPoint.v);
  132.     } else {
  133.         // No CDM so we use the low memory globals
  134.         *(Point *)xRawMouse = newPoint;
  135.         *(Point *)xMTemp = newPoint;
  136.         *(Ptr)xCrsrNew = *(Ptr)xCrsrCouple; // Set CrsrNew if coupled
  137.         CallCursorTask();    // must call jCrsrTask to update system
  138.     }
  139. }
  140.  
  141. void SetMouseDevicePositionRel(Point newPoint)
  142. {
  143.     // Adds the passed point to the current mouse location
  144.     CursorDevice *firstMouse;
  145.     Point tempPt;
  146.     if (TrapAvailable(_CursorDeviceDispatch)) { 
  147.         // If we get here we have the CDM
  148.         firstMouse = NULL;    // start at head of cursor dev list
  149.         CursorDeviceNextDevice(&firstMouse);    // get the next cursor device
  150.         // Call CDM to move the mouse relative
  151.         CursorDeviceMove(firstMouse,(long)newPoint.h,(long)newPoint.v);
  152.     } else {
  153.         // No CDM so we use the low memory globals
  154.         tempPt = *(Point *)xRawMouse;
  155.         tempPt.h += newPoint.h;
  156.         tempPt.v += newPoint.v;
  157.         *(Point *)xRawMouse = tempPt;
  158.         *(Point *)xMTemp = tempPt;
  159.         *(Ptr)xCrsrNew = *(Ptr)xCrsrCouple;
  160.         CallCursorTask();
  161.     }
  162.     
  163. void FudgeCursor(void)
  164. {
  165. // Now here is a routine that you can drop into the traffic light sample that
  166. // can be called to slowly move the mouse to the lower right hand corner of 
  167. // the screen it exits when the mouse button is held down
  168.  
  169.     Point RandPt;
  170.     long fred;
  171.  
  172.     GetMouseDevicePosition(&RandPt);
  173.     do {
  174.     RandPt.h += 1; // Bump to the next pixel across
  175.     RandPt.v += 1; // Bump to the next pixel down
  176.     
  177.     SetMouseDevicePosition(RandPt); // move absolute
  178.     
  179.     Delay(10,&fred); // wait 10 ticks for smooth drawing
  180.     } while (!Button()); // quit when the button is down.
  181. }
  182.